package com.ssyc.gsk_teacher.activity;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.utils.TextUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.base.Constants;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.BqaManager;
import com.ssyc.common.manager.SrfManager;
import com.ssyc.common.manager.StateBarManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.adapter.ApplyRvAdapter;
import com.ssyc.gsk_teacher.bean.ApplyInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 报名人数
 * Created by Administrator on 2018/7/9 0009.
 */

public class TeacherApplyPeopleActivity extends BaseActivity implements View.OnClickListener, SrfManager.OnPullRefreshListener {
    private RelativeLayout rlBack;
    private RecyclerView rv;
    private SmartRefreshLayout srf;
    private LinearLayout llConent;
    private RelativeLayout rlLoading;
    private String rowId;

    //数据集合
    private List<ApplyInfo.ListBean> oldDatas;
    //适配器
    private ApplyRvAdapter adapter;
    //空视图
    private View emptyView;

    //头布局
    private View headView;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return "";
    }

    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_apply_people;
    }

    @Override
    protected void init() {
        showContent();

        //配置状态栏颜色
        initStateColor();


        //获取rowid
        initIntent();

        //初始化视图
        initView();

        //初始化下拉刷新控件
        initSrf();

        //初始化列表控件
        initRv();

        //请求服务器数据
        http(BqaManager.NORML);
    }

    /**
     * 获取传递的rowId
     */
    private void initIntent() {
        rowId = getIntent().getStringExtra("rowid");
    }

    /**
     * 初始化视图
     */
    public void initView() {
        rlBack = findViewById(R.id.rl_back);
        rlBack.setOnClickListener(this);
        rv = findViewById(R.id.rv);
        srf = findViewById(R.id.srf);
        llConent = findViewById(R.id.ll_conent);
        rlLoading = findViewById(R.id.rl_loading);
        rlLoading.setVisibility(View.VISIBLE);
        llConent.setVisibility(View.GONE);
    }


    /**
     * 配置状态栏颜色的方法
     */
    private void initStateColor() {
        //设置6.0以后沉浸状体栏的问题
        StateBarManager.setBarWithUltimateBar(this, getResources().getColor(com.ssyc.common.R.color.titlebg),
                getResources().getColor(com.ssyc.common.R.color.nav_color));
    }


    /**
     * 初始化下拉刷新控件
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, this);
    }

    /**
     * 初始化列表控件
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        headView = View.inflate(this, R.layout.teacher_headview_apply, null);
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view_white_bg, null);
        adapter = new ApplyRvAdapter(this, R.layout.teacher_rv_apply, oldDatas);
        adapter.addHeaderView(headView);
    }

    /**
     * 请求服务器数据
     */
    private void http(int method) {
        Map<String, String> map = new HashMap<>();
        map.put("type", "11");
        map.put("acc", AccountUtils.getAccount(BaseApplication.context));
        map.put("role",  AccountUtils.getRole(BaseApplication.context));
        map.put("rowid", rowId);
        HttpUtils.post(HttpAdress.APPLY, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }
                if (llConent != null && llConent.getVisibility() != View.VISIBLE) {
                    llConent.setVisibility(View.VISIBLE);
                }

                if (!TextUtils.isEmpty(response)) {
                    ApplyInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, ApplyInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != info) {
                        if ("200".equals(info.getState())) {

                            //返回成功
                            List<ApplyInfo.ListBean> newDatas = info.getList();
                            if (newDatas != null) {
                                if (method == BqaManager.NORML) {
                                    oldDatas.clear();
                                    oldDatas.addAll(newDatas);
                                    BqaManager.setRv(emptyView, TeacherApplyPeopleActivity.this, adapter, rv);
                                } else {
                                    //更新数据
                                    BqaManager.updateRvBySmf(method, srf, rv, oldDatas, newDatas, adapter);
                                    adapter.setEnableLoadMore(false);
                                }
                            }

                        } else {
                            UiUtils.Toast(Constants.ERRORSTATE + info.getState(), false);
                            Log.i("test", Constants.ERRORSTATE + info.getState());
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }

    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH);
    }

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.rl_back) {
            // 返回
            finish();
        }
    }

}
